﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace BenefitHelper.Data.Week
{
    /// <summary>
    /// 操作员周收益排名
    /// </summary>
    public class OperatorWeekSort
    {
        public int Id { get; set; }

        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        public int Year { get; set; }

        public int Month { get; set; }

        public int Week { get; set; }

        public int Sort { get; set; }



        /// <summary>
        /// 初始化操作员周收益排名
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void InitOperatorWeekSort(int tradeHistoryId, DB.DBManager db)
        {
            Sys.TradeHistory th = new Sys.TradeHistory();
            th = db.TradeHistorys.Find(tradeHistoryId);
            List<Sys.TradeHistory> historys = th.GetWeekTradeingDay(th.WPdate,th.YPdate, db);
            List<View.OperatorDayChange> weekchanges = new List<View.OperatorDayChange>();



            foreach (Sys.TradeHistory history in historys)
            {
                List<View.OperatorDayChange> changes = new View.OperatorDayChange().GetOperatorDayChanges(history.Id, db);
                foreach (View.OperatorDayChange change in changes)
                {
                    var query = weekchanges.Where(a => a.OperatorId == change.OperatorId);
                    if (query.Count() > 0)//列表中已存在改用户的本周某天的记录
                    {
                        View.OperatorDayChange c = query.First();
                        query.First().Profit += change.Profit;
                    }
                    else//第一次加载用户某天记录
                    {
                        weekchanges.Add(change);
                    }
                }
            }

            var _query = weekchanges.OrderByDescending(a => a.Profit);
            int sort = 1;
            if (_query.Count() > 0)
            {
                foreach (View.OperatorDayChange c in _query)
                {

                    var existQuery = db.OperatorWeekSort.Where(a => a.OperatorId == c.OperatorId).Where(a => a.Year == th.YPdate).Where(a => a.Week == th.WPdate).ToList();
                    if (existQuery.Count() > 0)
                    {
                        existQuery.First().Sort = sort;
                    }
                    else
                    {
                        Data.Week.OperatorWeekSort weeksort = new OperatorWeekSort();
                        weeksort.Month = th.MPdate;
                        weeksort.OperatorId = c.OperatorId;
                        weeksort.Sort = sort;
                        weeksort.Week = th.WPdate;
                        weeksort.Year = th.YPdate;
                        db.OperatorWeekSort.Add(weeksort);
                    }



                    sort++;
                }
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 得到交易员周排名
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="operatorid"></param>
        /// <returns></returns>
        public int GetOperatorSort(int tradeHistoryId, int operatorid, DB.DBManager db)
        {
            int sort = 0;
            Sys.TradeHistory th = new Sys.TradeHistory();
            th = db.TradeHistorys.Find(tradeHistoryId);
            var query = db.OperatorWeekSort.Where(a => a.OperatorId == operatorid).Where(a => a.Year == th.YPdate).Where(a => a.Week == th.WPdate).ToList();
            if (query.Count() > 0)
            {
                sort = query.First().Sort;
            }
            return sort;
        }
    }
}
